package com.liu.communitysystemend.module.license.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.liu.communitysystemend.exception.CommonException;
import com.liu.communitysystemend.exception.SavaException;
import com.liu.communitysystemend.module.license.domain.License;
import com.liu.communitysystemend.module.license.domain.LicenseDto;
import com.liu.communitysystemend.module.license.domain.QLicenseDto;
import com.liu.communitysystemend.module.license.mapper.LicenseMapper;
import com.liu.communitysystemend.module.license.service.LicenseService;
import com.liu.communitysystemend.module.user.domain.User;
import com.liu.communitysystemend.module.user.domain.UserDto;
import com.liu.communitysystemend.module.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
*
*/
@Service
public class LicenseServiceImpl implements LicenseService {

    @Autowired
    private LicenseMapper licenseMapper;
    @Autowired
    private UserService userService;


    @Override
    public License getVisitorById(String id) {
        if (id == null) {
            throw new CommonException(501, "参数为空");
        }
        LambdaQueryWrapper<License> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(License::getId,id);
        License license = licenseMapper.selectOne(wrapper);
        return license;
    }
    public License selectOneById(String id){
        License license=licenseMapper.selectById(id);
        return  license;
    }

    @Override
    public License update(License license) {
        LambdaQueryWrapper<License> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(License::getId,license.getId());
        int update = licenseMapper.update(license, wrapper);
        if (update>0){
            return license;
        }
        throw new CommonException(502,"更新失败");
    }

    @Override
    public License add(License license) {
        if(license ==null){
            throw new SavaException("插入用户失败,菜单的类型实体为空");
        }
        int insert=licenseMapper.insert(license);
        if(insert>0){
            return license;
        }
        throw new SavaException("插入用户失败");
    }

    @Override
    public Page<LicenseDto> getPage(License license) {
        Page<LicenseDto> userDtoPage = new Page<>(license.getPage().getPage(),license.getPage().getSize());
        return licenseMapper.getAllPage(userDtoPage,license);
    }

    @Override
    public Integer deleteById(String id) {
        License license = selectOneById(id);
        if(license==null){
            throw new CommonException(4004,"找不到id为:"+id+"的数据");
        }
        int i=licenseMapper.deleteById(id);
        return i;
    }

    @Override
    public License selectByName(String licenseName) {
        LambdaQueryWrapper<License> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(License::getCarLicense,licenseName);
        License license = licenseMapper.selectOne(wrapper);
        return license;
    }

    @Override
    public Integer test() {
        for (int i = 0; i < 20; i++) {
            License license = new License();
            license.setId("100"+i);
            license.setCarLicense("粤B12U"+i);
            license.setUserId("100"+i);
            licenseMapper.insert(license);
        }
        return 1;
    }

    @Override
    public License addByCertNo(QLicenseDto qLicenseDto) {
        License license = new License();
        if (qLicenseDto.getCertNo()=="" || qLicenseDto.getCertNo()==null){
            license.setCarLicense(qLicenseDto.getLicense());
            add(license);
        }else{
            User user = userService.selectByIdCard(qLicenseDto.getCertNo());
            if (user==null){
                throw new CommonException(500,"请输入已注册用户的身份证或身份证账号有误");
            }
            license.setCarLicense(qLicenseDto.getLicense());
            license.setUserId(user.getId());
            add(license);
        }
        return license;
    }
}

